<?php
class NewsController extends AppController {

	var $name = 'News';
	var	$paginate = array(
              'limit' => 10,
              'order' => array(
                'News.id' => 'desc'
                )
              );
		
	function index() {
		$this->News->recursive = 0;
		$this->set('news', $this->paginate());
	}
	function _find_list() {
		return $this->News->Category->generatetreelist(null,null,null,'__');
		
	}
	function home() {
		$this->layout ='home';
		//get featured_slider
		$featured_slider = $this->get_all_news_index(null,null);
		$this->set('featured_slider' , $featured_slider);
		
		//get belowfeatured
		$this->set('belowfeatured' ,$this->get_cat_limit($featured_slider,19,1));
		//---------------------------------------------
		//home_mediabars
		$this->set('home_mediabars' , $this->get_cat_limit($featured_slider,19,18));
		//get bottombox_narrow

		$this->set('bottombox_narrow_1' , $this->get_cat_limit($featured_slider,19,5));
		$this->set('bottombox_narrow_2' ,$this->get_cat_limit($featured_slider,19,5));
		//get primarymid
		$this->set('primarymid' , $this->get_cat_limit($featured_slider,19,5));
		//get home_sidebar2
		$this->set('home_sidebar2' , $this->get_cat_limit($featured_slider,19,5));
		//home_mediabar
		$this->set('home_mediabar' , $this->get_all_news_index(19,6));
		 //Get Latest
		 $this->set('set_Latest' , $this->get_cat_limit($featured_slider,19,5));
		 //get set_Popular
		 $this->set('set_Popular' , $this->get_cat_limit($featured_slider,19,5));
		 $this->set('recentcomments' , $this->News->Comment->find('all',array(
			'limit' => '5',
			'order' => array(
				'Comment.id' => 'desc'
			)
		 )));
	}
		function get_cat_limit($data = null,$cat = null, $limit = null){
		$i =0;
		if($cat == null)
			$cat = 1;
		if($limit == null)
			$limit = 10;		
		$results = array();
		foreach($data as $set_data){
			if($set_data['Category']['id'] == $cat && ( $i < $limit)){
				$results[$i] = $set_data;
				$i++;
			}// 
		}
		return $results;
	}
	/* menu
	--------------------------*
	/** Get news 
	----------------------*/
	function get_all_news_index($cat = null, $limmit = null){
		$this->News->recursive = 2;
		if($limmit == null)
			$limmit = 5;
		$paginate = array(
              'limit' => $limmit,
              'order' => array(
                'News.created' => 'desc'
                )
              );
		if(isset($cat)){
			$this->paginate['conditions'] = array(
						'News.category_id'=>$cat		
			);
		}
		return  $this->paginate();
	}
	
	function show($id =null){
		$this->layout ='home';
		if (!$id) {
			$this->Session->setFlash(__('Invalid news', true));
			$this->redirect(array('action' => 'home'));
		}
		$news = $this->News->read(null, $id);
		if(isset($news['Comment'])){
			$i = 0;
			foreach($news['Comment'] as $set_cọmment){
				if($set_cọmment['user_id']){
					$get_user = $this->get_user_by_id($set_cọmment['user_id']);
					if($get_user){
						$news['Comment'][$i]['User'] = $get_user;
					}
					else{
						$news['Comment'][$i]['User'] = null;
					}
				}
				else{
					$news['Comment'][$i]['User'] = null;
				}
				$i++;
			}
		}
		$this->set('news', $news);
					//get primarymid
		$this->set('primarymid' , $this->get_all_news_index(null,null));
		 //Get Latest
		 $this->set('set_Latest' , $this->get_all_news_index(null,null));
		 //get set_Popular
		 $this->set('set_Popular' , $this->get_all_news_index(null,null));
		 $this->set('recentcomments' , $this->News->Comment->find('all',array(
			'limit' => '5',
			'order' => array(
				'Comment.id' => 'desc'
			)
		 )));
	}
	
	
	function category($cat_id = null) {
		$this->layout ='home';
		if (!$cat_id) {
			//$this->Session->setFlash(__('Invalid news', true));
			$this->redirect(array('action' => 'home'));
		}
		$this->News->recursive = 2;
		$paginate = array(
              'limit' => 10,
              'order' => array(
                'News.id' => 'desc'
                )
              );
		 $this->paginate['conditions'] = array(
					'News.category_id'=>$cat_id		
			);
		$get_all_news = $this->paginate();
		$this->set('news_index', $get_all_news);
			//get primarymid
		$this->set('primarymid' , $this->get_all_news_index(null,null));
		 //Get Latest
		 $this->set('set_Latest' , $this->get_all_news_index(null,null));
		 //get set_Popular
		 $this->set('set_Popular' , $this->get_all_news_index(null,null));
		 $this->set('recentcomments' , $this->News->Comment->find('all',array(
			'limit' => '5',
			'order' => array(
				'Comment.id' => 'desc'
			)
		 )));
	}
	
	function auth($user_id = null) {
		$this->layout ='home';
		if (!$user_id) {
			//$this->Session->setFlash(__('Invalid news', true));
			$this->redirect(array('action' => 'home'));
		}
		$this->News->recursive = 2;
		$paginate = array(
              'limit' => 10,
              'order' => array(
                'News.id' => 'desc'
                )
              );
		$this->paginate['conditions'] = array(
					'News.user_id'=>$user_id		
		);
		$this->set('news_index_auth', $this->paginate());
					//get primarymid
		$this->set('primarymid' , $this->get_all_news_index(null,null));
		 //Get Latest
		 $this->set('set_Latest' , $this->get_all_news_index(null,null));
		 //get set_Popular
		 $this->set('set_Popular' , $this->get_all_news_index(null,null));
		 $this->set('recentcomments' , $this->News->Comment->find('all',array(
			'limit' => '5',
			'order' => array(
				'Comment.id' => 'desc'
			)
		 )));
	}
	
	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid news', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('news', $this->News->read(null, $id));
	}

	function add() {
		if (!empty($this->data)) {
			//var_dump($this->data);
			$this->News->create();
			
			$this->data['News']['user_id'] = $_SESSION['Auth']['User']['id'];//$this->Session->Auth['User']['id'];
			if(!empty($this->data['News']['image'])){
				$fileOK = $this->uploadFiles('img/uploads', $this->data['News']['image']);
				$this->data['News']['image'] = $fileOK['urls'][0];
			}

			if ($this->News->save($this->data)) {
				$this->Session->setFlash(__('The news has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The news could not be saved. Please, try again.', true));
			}
		}
		$categories = $this->_find_list();
		//$categories = $this->News->Category->find('list');
		$users = $this->News->User->find('list');
		$this->set(compact('categories', 'users'));
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid news', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			$this->data['News']['user_id'] = $_SESSION['Auth']['User']['id'];//$this->Session->Auth['User']['id'];
			//$fileOK = $this->uploadFiles('img/uploads', $this->data['News']['image_new']);
			if(isset($this->data['News']['image_new']['name']) && !empty($this->data['News']['image_new']['name'])){
				$fileOK = $this->uploadFiles('img/uploads', $this->data['News']['image_new']);
				if(isset($fileOK['urls'][0])){
					$this->data['News']['image'] = $fileOK['urls'][0];
				}
			}
			if ($this->News->save($this->data)) {
				$this->Session->setFlash(__('The news has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The news could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->News->read(null, $id);
		}
		$categories = $this->News->Category->find('list');
		$users = $this->News->User->find('list');
		$this->set(compact('categories', 'users'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for news', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->News->delete($id)) {
			$this->Session->setFlash(__('News deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('News was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
        
	function active($id = null){
		if (!$id) {
			$this->Session->setFlash(__('Tin tức không tồn tại', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->News->create();
		$this->data['News']['id'] = $id;
		$this->data['News']['status'] = 1;
		if ($this->News->save($this->data)) {
			$this->Session->setFlash(__('Trạng thái được kích hoạt', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Trạng thái cập nhật thất bại', true));
		$this->redirect(array('action' => 'index'));
	}
	
	function unactive($id =null) {

		if (!$id) {
			$this->Session->setFlash(__('Tin tức không tồn tại', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->News->create();
		$this->data['News']['id'] = $id;
		$this->data['News']['status'] = 0;   
		var_dump($this->data);
		if ($this->News->save($this->data)) {
			$this->Session->setFlash(__('Trạng thái được hủy kích hoạt', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Trạng thái cập nhật thất bại', true));
		$this->redirect(array('action' => 'index'));

	}
}
